<?php
/**
 * @table paysystems
 * @id 4csonline
 * @title 4csonline
 * @visible_link https://www.4csonline.com/
 * @recurring none
 * @country KN
 * @am_payment_api 6.0
 */
class Am_Paysystem_4csonline extends Am_Paysystem_CreditCard
{
    const PLUGIN_STATUS = self::STATUS_DEV;
    const PLUGIN_DATE = '$Date$';
    const PLUGIN_REVISION = '6.3.32';

    const LIVE_URL = "https://merchants.4csonline.com/TranSvcs/ssis.asmx";
    const SANDBOX_URL = 'https://merchants.4csonline.com/DevTranSvcs/ssis.asmx';

    protected $defaultTitle = "4csonline";
    protected $defaultDescription = "accepts all major credit cards";
    protected $id = "4csonline";

    public function _initSetupForm(Am_Form_Setup $form)
    {
        $id = $this->getId();
        $form->addText("key")->setLabel('Merchant key');
        $form->addAdvCheckbox("testing")->setLabel("Test Mode Enabled");
    }
    public function getSupportedCurrencies()
    {
        return ['USD'];
    }

    public function getRecurringType()
    {
        return self::REPORTS_NOT_RECURRING;
    }

    public function _doBill(Invoice $invoice, $doFirst, CcRecord $cc, Am_Paysystem_Result $result)
    {
        $txnid = $invoice->invoice_id . uniqid();
        $invoice_num = $invoice->public_id . $this->getDi()->security->randomString(4);
        $xml = '<?xml version="1.0" encoding="utf-8"?>
            <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
            <soap:Body><SSISProcessTransaction xmlns="http://equament.com/Schemas/Fmx/ssis">
            <request>
            <MerchantKey>'.$this->getConfig('key').'</MerchantKey>
            <TranType>AuthOnly</TranType>
            <Amount>'.$invoice->first_total.'</Amount>
            <Currency>'.$invoice->currency.'</Currency>
            <Invoice>'.$invoice_num.'</Invoice>
            <TranId>'.$txnid.'</TranId>
            <CardNumber>'.$cc->cc_number.'</CardNumber>
            <ExpiryMMYY>'.$cc->cc_expire.'</ExpiryMMYY>
            <VerificationValue>'.$cc->getCvv().'</VerificationValue>
            <CardholderName>'.$cc->cc_name_f.' '.$cc->cc_name_l.'</CardholderName>
            <Address>'.$cc->cc_street.' '.$cc->cc_city.' '.$cc->cc_state.' '.$cc->cc_zip.' '.$cc->cc_country.'</Address>
            </request>
            </SSISProcessTransaction>
            </soap:Body>
            </soap:Envelope>';
        ;
        $request = $this->createHttpRequest();
        $request->setUrl($this->getConfig('testing') ? self::SANDBOX_URL : self::LIVE_URL);
        $request->setMethod(Am_HttpRequest::METHOD_POST);
        $request->setBody($xml);
        $request->setHeader('Content-type', 'text/xml');
        $transaction = new Am_Paysystem_Transaction_4csonline($this, $invoice, $request, $doFirst);
        $transaction->run($result);
    }
}
class Am_Paysystem_Transaction_4csonline extends Am_Paysystem_Transaction_CreditCard
{

    function processValidated()
    {
        $this->invoice->addPayment($this);
    }
    public function parseResponse()
    {
        $xml = simplexml_load_string($this->response->getBody());
        $this->xml = $xml->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://equament.com/Schemas/Fmx/ssis');
    }

    public function getUniqId()
    {
        return (string)$this->xml->SSISProcessTransactionResponse->response->ApprovalCode;
    }
    public function validate()
    {
        if ((string)$this->xml->SSISProcessTransactionResponse->response->ResultCode == 'OK' &&
            (string)$this->xml->SSISProcessTransactionResponse->response->FinancialResultCode == 'Approved')
            $this->result->setSuccess($this);
        elseif($err = (string)$this->xml->SSISProcessTransactionResponse->response->ErrorMessage)
            $this->result->setFailed($err);
        else
            $this->result->setFailed(___('Payment failed'));
    }
}
